//系统类型
var sysType;

$(function () {
    $('#OA').css('display', 'none');
    showModuleTree();
    sysType = "crmSysType";
});

var setting = {
    view: {
        showLine: true,//显示节点之间的连线。
        selectedMulti: false  //允许同时选中多个节点。
    },
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pId",
            rootPId: 0
        }
    },
    check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: { "Y": "ps", "N": "s" }
    }
};

//创建权限组--系统切换tab
function changeSysTab(type) {
    if (type == 1) {
        $('.reserve').addClass('active');
        $('.interview').removeClass('active');
        $('#CRM').css('display', 'flex');
        $('#OA').css('display', 'none');
        //将系统类型标识设置为crm系统
        sysType = "crmSysType";
    }
    if (type == 2) {
        $('.interview').addClass('active');
        $('.reserve').removeClass('active');
        $('#OA').css('display', 'flex');
        $('#CRM').css('display', 'none');
        //将系统类型标识设置为oa系统
        sysType = "oaSysType";
    }
}

/**
 *展示创建权限组菜单树
 * @param row
 */
function showModuleTree() {
    $.ajax({
        type:"post",
        url:"/permissionManagement/getPermissionTree",
        dataType:"json",
        success:function(data){
            if (data.code == '000000') {
                var obj = data.data;
                var zNodesCrmManagement = obj.managementModule;
                $.fn.zTree.init($("#crmManagementModuleTree"), setting, zNodesCrmManagement);
                var zNodesCrmUser = obj.userModule;
                $.fn.zTree.init($("#crmUserModuleTree"), setting, zNodesCrmUser);
            } else {
                $.alert({
                    title: '温馨提示',
                    content: '加载失败',
                });
            }
        }
    });
}

/**
 * 创建权限组提交事件
 */
function addPermissionGroupTab(){
    var createPermissionGroupJson = {};
    var selectCheckedNodesArray = [];//选中节点数据数组
    /** 权限组信息  */
    var permissionGroupMsg = {};
    var permissionGroupName = $('#permissionGroupName').val();//权限组名
    var remark = $('#remark').val();//备注
    permissionGroupMsg.permissionGroupName = permissionGroupName;
    permissionGroupMsg.remark = remark;

    /** 选中节点信息  */
    //系统类型(crm)
    if(sysType == "crmSysType"){
        permissionGroupMsg.sysId = "2";
        createPermissionGroupJson.permissionGroupMsg = permissionGroupMsg;
        //CRM管理模块
        var crmManagerTreeObj = $.fn.zTree.getZTreeObj("crmManagementModuleTree");
        var selectCrmManagerNodes = crmManagerTreeObj.getCheckedNodes(true);
        if(selectCrmManagerNodes.length > 0) {
            for(var index = 0;index < selectCrmManagerNodes.length;index++){
                var selectCheckedNode = {};
                var tmpCheckedNode = selectCrmManagerNodes[index];
                selectCheckedNode.groupId = tmpCheckedNode.groupId;//菜单分组
                selectCheckedNode.nodeId = tmpCheckedNode.id;//节点ID
                selectCheckedNode.menuType = tmpCheckedNode.menuType;//菜单类型(0--一级菜单;1--二级菜单;2--全部地区;3--省份;4--公司)
                //当前节点是否为半选节点 0：一个子节点也没选 1:半选 2：全选
                selectCheckedNode.isCheckAll = tmpCheckedNode.check_Child_State;
                selectCheckedNodesArray.push(selectCheckedNode);
            }
        }

        //CRM用户模块
        var crmUserTreeObj = $.fn.zTree.getZTreeObj("crmUserModuleTree");
        var selectCrmUserNodes = crmUserTreeObj.getCheckedNodes(true);
        if(selectCrmUserNodes.length > 0) {
            for(var index = 0;index < selectCrmUserNodes.length;index++){
                var selectCheckedNode = {};
                var tmpCheckedNode = selectCrmUserNodes[index];
                selectCheckedNode.groupId = tmpCheckedNode.groupId;//菜单分组
                selectCheckedNode.nodeId = tmpCheckedNode.id;//节点ID
                selectCheckedNode.menuType = tmpCheckedNode.menuType;//菜单类型(0--一级菜单;1--二级菜单;2--全部地区;3--省份;4--公司)
                //当前节点是否为半选节点 0：一个子节点也没选 1:半选 2：全选
                selectCheckedNode.isCheckAll = tmpCheckedNode.check_Child_State;
                selectCheckedNodesArray.push(selectCheckedNode);
            }
        }
        createPermissionGroupJson.selectCheckedNodesArray = selectCheckedNodesArray;
    }

    /** 选中节点信息  */
    //系统类型(oa)
    if(sysType == "oaSysType"){

    }

    $.ajax({
        url: "/permissionManagement/addPermissionGroup",
        type: "POST",
        dataType: "json",
        data: {
            "createPermissionGroupJson": JSON.stringify(createPermissionGroupJson)
        },
        async: false,
        success: function (data) {
            if (data.code == '000000') {
                $.alert('操作成功!');
                var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
                var navTab = parent.$multitabs._getNavTab($thisTabs);
                openTab("权限组名单","/front/permissionManager/permissionList");
                refreshActivePane("/front/permissionManager/permissionList");
                delStorageAndRemove(navTab);
            } else {
                $.alert('操作失败,请稍后再试！');
            }
        },
        error: function () {
        }
    })
}

function cancelPermissionGroup(){
    window.location.reload();
}